package project.controller;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import project.entity.TJdlx;
import project.entity.TSpots;
import project.service.TSpotsService;
import project.util.ResponseUtil;
import project.util.UUIDUtil;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 旅游景点 前端控制器
 * </p>
 *
 * @author YJR
 * @since 2023-07-25
 */
@RestController
@RequestMapping("/api/spots/v1")
public class TSpotsController {
    @Resource
    private TSpotsService spotsService;
    /**
     * 添加或者更新旅游景点
     * @param tSpots
     * @return
     */
    @RequestMapping(value = "saveOrUpdate",method = RequestMethod.POST)
    public boolean saveOrUpdate(@RequestBody TSpots tSpots){
        if(StringUtils.isBlank(tSpots.getcId())){
            tSpots.setcId(UUIDUtil.getUUID());
        }
        return spotsService.saveOrUpdate(tSpots);
    }
    /**
     * 添加或者更新旅游景点
     * @param tSpots
     * @return
     */
    @RequestMapping(value = "wx/saveOrUpdate",method = RequestMethod.POST)
    public boolean wxSaveOrUpdate(@RequestBody TSpots tSpots){
//        TSpots tSpots = param.to(TSpots.class);
        if(StringUtils.isBlank(tSpots.getcId())){
            tSpots.setcId(UUIDUtil.getUUID());
        }
        return spotsService.saveOrUpdate(tSpots);
    }


    /**
     * 分页查询
     * @param param
     * @return
     */
    @RequestMapping(value="/select",method = RequestMethod.POST)
    public JSONObject selectSp(@RequestBody JSONObject param){
        // 获取页数
        Integer current = param.getInteger("current");
        // 景点名称
        String jdmc = param.getString("jdmc");

        // 景点分类
        String jdfl = param.getString("jdfl");
        Page<TSpots> page = new Page<TSpots>();
        // 设置每页数量
        page.setSize(10);

        if(current != null){
            page.setCurrent(current);
        }else{
            page.setCurrent(1);
        }

        QueryWrapper<TSpots> wrapper = new QueryWrapper<>();
        // 景点分类
        if(StringUtils.isNotBlank(jdfl)){
            wrapper.lambda().eq(TSpots::getcJdlxId,jdfl);
        }
        // 景点名称
        if(StringUtils.isNotBlank(jdmc)){
            wrapper.lambda().like(TSpots::getcName,jdmc);
        }

        // 获取查询返回结果
        Page<TSpots> pageSelect = spotsService.page(page, wrapper);
        JSONObject object = new JSONObject();
        object.put("list",pageSelect.getRecords());
        object.put("total",pageSelect.getTotal());
        return ResponseUtil.getSuccessResponseBody(object);
    }

    @RequestMapping(value = "/del",method = RequestMethod.POST)
    public boolean del(@RequestBody TJdlx tJdlx){
        return spotsService.removeById(tJdlx);
    }

    @RequestMapping(value = "/wx/getAll",method = RequestMethod.POST)
    public List<TSpots> getAll(@RequestBody JSONObject param){
        String cId = param.getString("cId");
        String jdlxId = param.getString("jdlxId");
        String jdmc = param.getString("jdmc");
        QueryWrapper<TSpots> wrapper = new QueryWrapper<>();
        if(StringUtils.isNotBlank(jdlxId)){
            wrapper.lambda().eq(TSpots::getcJdlxId,jdlxId);
        }
        if(StringUtils.isNotBlank(cId)){
            wrapper.lambda().eq(TSpots::getcId,cId);
        }
        if(StringUtils.isNotBlank(jdmc)){
            wrapper.lambda().like(TSpots::getcName,jdmc);
        }
        return spotsService.list(wrapper);
    }
}
